package com.woniu.gf.pawn.comm.sentinel.exception;

import com.woniu.gf.pawn.comm.core.exception.ErrorResult;
import com.woniu.gf.pawn.comm.core.exception.PawnException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.time.LocalDateTime;

/**
 * @BelongsProject: gf-pawn-comm
 * @BelongsPackage: com.woniu.gf.pawn.comm.sentinel.exception.handler
 * @Author: wu wei
 * @CreateTime: 2020-10-08 20:42
 * @Description:
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(value = PawnException.class)
    public ResponseEntity<ErrorResult> errorHandler(PawnException e){
        log.error("出现异常：{}，时间：{},说明：{}",e.getClass().getName(), LocalDateTime.now().toString(),e.getMessage());
        ErrorResult er = new ErrorResult(e.getCode(), e.getMessage());
        return new ResponseEntity<>(er, HttpStatus.INTERNAL_SERVER_ERROR);
    }
    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<ErrorResult> errorHandler(Exception e){
        log.error("出现异常：{}，时间：{},说明：{}",e.getClass().getName(), LocalDateTime.now().toString(),e.getMessage());
        ErrorResult er = new ErrorResult(500, e.getMessage());
        return new ResponseEntity<>(er, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
